﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;

namespace SPNetworkTopology
{
    class Utility
    {
         string AddInName = "SharePointNetworkTopology";

         public string ConfigFilePath
         {
             get
             {
                 return this.AppPath() + @"\SPNetworkTopology.dll";
             }
         }      

         public string StencilName
         {
             get
             {
                 return this.AppPath() + @"\Visio Templates\SharePoint Topology.vss"; 
             }
         }        

        /// <summary>
        /// Get the application installed path
        /// </summary>
        /// <returns></returns>
        private string AppPath()
        {
            RegistryKey regKey = Registry.CurrentUser.OpenSubKey(String.Format("Software\\Microsoft\\Visio\\Addins\\{0}", this.AddInName));
            string manifestLocation = string.Empty;
            if (regKey != null)
            {
                manifestLocation = regKey.GetValue("Manifest").ToString();
            }

            string directoryName = manifestLocation.Contains('|') ?
                Path.GetDirectoryName(manifestLocation.Remove(manifestLocation.IndexOf('|'))) :
                 Path.GetDirectoryName(manifestLocation);

            if (!directoryName.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
            {
                directoryName += System.IO.Path.DirectorySeparatorChar.ToString();
            }

            directoryName = directoryName.Contains(@"file:\") ? directoryName.Replace(@"file:\", "") : directoryName;
            // return result
            return directoryName;
        }
    }
}
